<template>
  <el-dialog
    v-model="dialogVisible"
    title="图片预览"
    width="80%"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div class="picture-container">
      <img v-if="dialogImgurl" :src="dialogImgurl" alt="预览图片" class="preview-img" />
      <div v-if="picInfo" class="pic-info">
        <h4>图片信息</h4>
        <p v-if="picInfo.name">文件名: {{ picInfo.name }}</p>
        <p v-if="picInfo.size">文件大小: {{ picInfo.size }}</p>
        <p v-if="picInfo.width">宽度: {{ picInfo.width }}px</p>
        <p v-if="picInfo.height">高度: {{ picInfo.height }}px</p>
        <p v-if="picInfo.format">格式: {{ picInfo.format }}</p>
        <p v-if="picInfo.createTime">创建时间: {{ picInfo.createTime }}</p>
      </div>
    </div>
    <template #footer>
      <el-button @click="handleClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'

// Props
const props = defineProps({
  dialogPreviewImg: {
    type: Boolean,
    default: false
  },
  dialogImgurl: {
    type: String,
    default: ''
  },
  picInfo: {
    type: Object,
    default: () => ({})
  }
})

// Emits
const emit = defineEmits(['handleCloseImg'])

// 响应式数据
const dialogVisible = ref(false)

// 监听props变化
watch(() => props.dialogPreviewImg, (newVal) => {
  dialogVisible.value = newVal
})

// Methods
const handleClose = () => {
  dialogVisible.value = false
  emit('handleCloseImg')
}
</script>

<style scoped>
.picture-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.preview-img {
  max-width: 100%;
  max-height: 60vh;
  border-radius: 4px;
  margin-bottom: 20px;
}

.pic-info {
  width: 100%;
  background: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  text-align: left;
}

.pic-info h4 {
  margin-top: 0;
  margin-bottom: 10px;
  color: #303133;
}

.pic-info p {
  margin: 5px 0;
  color: #606266;
}
</style>